<?php

namespace App\Kuafu\Shop\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\User\Model\User;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;

/**
 * App\Kuafu\Shop\Model\ShopStaffRecordModel
 *
 * @property int $id
 * @property int $shopId 门店ID
 * @property int $shopCode 门店Code
 * @property int $userId 用户ID
 * @property int $inShop 是否需要到店  0-到店  1-不到店
 * @property int $auditStatus 审核状态  0-过期/缺失 1-待审核 2-待驳回 3-已通过 4-无需到店 5-离职
 * @property string $causeOfRejection 驳回原因
 * @property int $healthCardStatus 健康证状态  0-缺失 1-已上传 2-即将到期 3-已过期 4-null
 * @property int $idCardStatus 身份证状态  0-null 1-缺失 2-已上传
 * @property int $positionStatus 职位状态  0-在职 1-离职
 * @property int $entryTime
 * @property int $leaveTime
 * @property int $lastAuditTime 最近审核时间
 * @property int $lastManagerUpdateTime 商家更新时间
 * @property int $createdByUid 创建人
 * @property int $updatedByUid 更新人
 * @property int $createdAt 创建时间
 * @property int $updatedAt 更新时间
 * @property-read string $audit_status_text
 * @property-read string $health_card_status_text
 * @property-read string $id_card_status_text
 * @property-read string $in_shop_text
 * @property-read string $position_status_text
 * @property-read \App\Kuafu\Shop\Model\Shop|null $shopRel
 * @property-read User|null $userRel
 */
class ShopStaffRecordModel extends BaseModel
{
    protected $connection = 'mysql';
    protected $table      = 'shop_staff_records';

    protected $fillable = [
        'shopId',
        'shopCode',
        'userId',
        'inShop',
        'auditStatus',
        'causeOfRejection',
        'healthCardStatus',
        'idCardStatus',
        'positionStatus',
        'entryTime',
        'leaveTime',
        'lastAuditTime',
        'lastManagerUpdateTime',
        'createdByUid',
        'updatedByUid',
        'createdAt',
        'updatedAt',
    ];

    protected $appends = [
        'inShopText',
        'auditStatusText',
        'healthCardStatusText',
        'idCardStatusText',
        'positionStatusText',
    ];

    /**
     * inShop 是否需要到店
     */
    const IN_SHOP_ON      = 0;
    const IN_SHOP_OFF     = 1;
    const IN_SHOP_MAPPING = [
        self::IN_SHOP_ON  => '到店',
        self::IN_SHOP_OFF => '不到店',
    ];

    /**
     * auditStatus 审核状态
     */
    const AUDIT_STATUS_EXPIRED_OR_LACK = 0;
    const AUDIT_STATUS_WAIT_AUDIT      = 1;
    const AUDIT_STATUS_REJECT          = 2;
    const AUDIT_STATUS_PASS            = 3;
    const AUDIT_STATUS_NOT_IN_SHOP     = 4;
    const AUDIT_STATUS_LEAVE           = 5;
    const AUDIT_STATUS_MAPPING         = [
        self::AUDIT_STATUS_EXPIRED_OR_LACK => '过期/缺失',
        self::AUDIT_STATUS_WAIT_AUDIT      => '待审核',
        self::AUDIT_STATUS_REJECT          => '已驳回',
        self::AUDIT_STATUS_PASS            => '已通过',
        self::AUDIT_STATUS_NOT_IN_SHOP     => '无需到店',
        self::AUDIT_STATUS_LEAVE           => '离职',
    ];


    /**
     * healthCardStatus 健康证状态
     */
    const HEALTH_CARD_STATUS_LACK            = 0;
    const HEALTH_CARD_STATUS_UPLOADED        = 1;
    const HEALTH_CARD_STATUS_ABOUT_TO_EXPIRE = 2;
    const HEALTH_CARD_STATUS_EXPIRED         = 3;
    const HEALTH_CARD_STATUS_NULL            = 4;
    const HEALTH_CARD_STATUS_MAPPING         = [
        self::HEALTH_CARD_STATUS_LACK            => '缺失',
        self::HEALTH_CARD_STATUS_UPLOADED        => '已上传',
        self::HEALTH_CARD_STATUS_ABOUT_TO_EXPIRE => '即将到期',
        self::HEALTH_CARD_STATUS_EXPIRED         => '已过期',
        self::HEALTH_CARD_STATUS_NULL            => '',
    ];


    /**
     * idCardStatus 身份证状态
     */
    const ID_CARD_STATUS_DEFAULT  = 0;
    const ID_CARD_STATUS_LACK     = 1;
    const ID_CARD_STATUS_UPLOADED = 2;
    const ID_CARD_STATUS_MAPPING  = [
        self::ID_CARD_STATUS_DEFAULT  => '',
        self::ID_CARD_STATUS_LACK     => '缺失',
        self::ID_CARD_STATUS_UPLOADED => '已上传',
    ];


    /**
     * positionStatus 职位状态
     */
    const POSITION_STATUS_ON      = 0;
    const POSITION_STATUS_OFF     = 1;
    const POSITION_STATUS_MAPPING = [
        self::POSITION_STATUS_ON  => '在职',
        self::POSITION_STATUS_OFF => '离职',
    ];

    const AUDIT_ACTION_REJECT   = 0;
    const AUDIT_ACTION_APPROVED = 1;

    const AUDIT_ACTION_MAP = [
        self::AUDIT_ACTION_REJECT   => '驳回',
        self::AUDIT_ACTION_APPROVED => '通过',
    ];

    //操作行为->状态map
    const AUDIT_ACTION_STATUS_MAP = [
        self::AUDIT_ACTION_REJECT   => self::AUDIT_STATUS_REJECT,
        self::AUDIT_ACTION_APPROVED => self::AUDIT_STATUS_PASS
    ];


    /**
     * 关联用户表
     */
    public function userRel(): HasOne
    {
        return $this->hasOne(User::class, 'id', 'userId');
    }

    /**
     * 关联门店表
     */
    public function shopRel(): HasOne
    {
        return $this->hasOne(Shop::class, 'id', 'shopId');
    }


    public static function healthCardStatus(int $healthCardEndDate)
    {
        if (empty($healthCardEndDate)) {
            return self::HEALTH_CARD_STATUS_LACK;
        }
        if ($healthCardEndDate < time()) {
            return self::HEALTH_CARD_STATUS_EXPIRED;
        }
        if ($healthCardEndDate > time() && $healthCardEndDate < Carbon::now()->addDays(30)->timestamp) {
            return self::HEALTH_CARD_STATUS_ABOUT_TO_EXPIRE;
        }

        return self::HEALTH_CARD_STATUS_UPLOADED;
    }










    /**
     * 访问器
     */
    public function getInShopTextAttribute(): string
    {
        return self::IN_SHOP_MAPPING[$this->attributes['inShop']] ?? '';
    }

    public function getAuditStatusTextAttribute(): string
    {
        return self::AUDIT_STATUS_MAPPING[$this->attributes['auditStatus']] ?? '';
    }

    public function getHealthCardStatusTextAttribute(): string
    {
        return self::HEALTH_CARD_STATUS_MAPPING[$this->attributes['healthCardStatus']] ?? '';
    }

    public function getIdCardStatusTextAttribute(): string
    {
        return self::ID_CARD_STATUS_MAPPING[$this->attributes['idCardStatus']] ?? '';
    }

    public function getPositionStatusTextAttribute(): string
    {
        return self::POSITION_STATUS_MAPPING[$this->attributes['positionStatus']] ?? '';
    }

}